package util

import (
	"bytes"
	"github.com/boombuler/barcode"
	"github.com/boombuler/barcode/qr"
	"image/png"
	"math/rand"
	"os"
	"strconv"
	"time"
)

func GenQRCode(fileName, content string) error {
	os.Remove(fileName)
	f, err := os.Create(fileName)
	defer f.Close()
	if err == nil {
		qrcode, err := qr.Encode(content, qr.L, qr.Auto)
		if err == nil {
			qrcode, err = barcode.Scale(qrcode, 300, 300)
			if err == nil {
				png.Encode(f, qrcode)
			}
		}
	}
	return err
}

func GenVerifyCode(len int) string {
	var buf bytes.Buffer
	rand.Seed(int64(time.Now().Nanosecond()))
	for i := 0; i < len; i++ {
		buf.WriteString(strconv.Itoa(rand.Intn(10)))
	}
	return buf.String()
}
